package test9;

public class StudentTest {
    public static void main(String[] args) {
        Student[] arry = new Student[3];

        Student st1 = new Student(1,"zhangsan",23);
        Student st2 = new Student(2,"lisi",21);


        arry[0] = st1;
        arry[1] = st2;


        Student st4 = new Student(4,"weizhihui",26);
        boolean flag = contains(arry,st4.getId());
        if (flag){
            System.out.println("重复Id");
        }else {
            int count = getCount(arry);
            if(count == arry.length){
                Student[] newarry = createArry(arry);
                newarry[count] = st4;
                printStudent(newarry);

            }else {
                arry[count] = st4;
                printStudent(arry);
            }
        }

    }
    public static void printStudent(Student[] arry){
        for (int i = 0; i < arry.length; i++) {
            Student student = arry[i];
            if(student != null){
                System.out.println(student.getId()+","+student.getName()+","+student.getAge());
            }
        }
    }

    public static Student[] createArry(Student[] arry){
        Student[] newarry = new Student[arry.length+1];
        for (int i = 0; i < arry.length; i++) {
            newarry[i] = arry[i];
        }
        return newarry;
    }
    public static int getCount(Student[] arry){
        int count = 0;
        for (int i = 0; i < arry.length; i++) {
            if(arry[i] != null){
                count++;
            }
        }
        return count;
    }

    public static boolean contains(Student[] arry,int id){
        for (int i = 0; i < arry.length; i++) {
            Student student = arry[i];
            if (student != null) {
                int sid = student.getId();
                if (sid == id) {
                    return true;
                }

            }
        }
        return false;
    }
}
